package work.linruchang.util;

import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import work.linruchang.util.bean.IDCardInfo;

import java.util.Date;
import java.util.Optional;

/**
 * @author LinRuChang
 * @version 1.0
 * @date 2022/09/20
 * @since 1.8
 **/
public class EnhanceDict extends Dict {



    public <T,M> String getStr(Func1<T,M> key) {
        return Optional.ofNullable(key)
                .map(LambdaUtil::getFieldName)
                .map(fieldName -> getStr(fieldName))
                .orElse(null);
    }


    public <T,M> M get(Func1<T,M> key) {
        return getByPath(LambdaUtil.getFieldName(key), EnhanceLambdaUtil.getMethodResultType(key) );
    }


    public <T,M> EnhanceDict set(Func1<T,M> attr, Object value) {
        set(LambdaUtil.getFieldName(attr),value);
        return this;
    }

    public static void main(String[] args) {

        EnhanceDict enhanceDict = new EnhanceDict();
        enhanceDict.set(IDCardInfo::getName,"嗯嗯*");
        enhanceDict.set(IDCardInfo::getCertificateValidityStartDate, new Date());


        String name = enhanceDict.get(IDCardInfo::getName);
        Console.log(name);
        Date date = enhanceDict.get(IDCardInfo::getCertificateValidityStartDate);
        Console.log(date);

    }

}
